/**
 * 会话相关操作
 */
import store from '@/store/index.js';
import tim from '@/utils/tim.js';
import TIM from '@/utils/tim-js-friendship.js';
import moment from 'moment';

/**
 * 把最后一条消息简短显示的转换
 * @params:lastMessage 参数格式
    {
		cloudCustomData: ""
		fromAccount: "chengcheng"
		isRevoked: false
		lastSequence: 1332220001
		lastTime: 1628141561
		messageForShow: "Hello world!"
		onlineOnlyFlag: false
		payload: {text: "Hello world!"}
		type: "TIMTextElem"
	} 
 
 */
export function lastMessageFormatShor(lastMessage = {}) {
	if (lastMessage && lastMessage.lastTime) {
		//文本消息
		if (lastMessage.type === TIM.TYPES.MSG_TEXT) {
			return {
				lastMsg: lastMessage.messageForShow,
				lastDate: moment(lastMessage.lastTime * 1000).format(
					'YYYY-MM-DD'
				),
			};
		}

		//图片消息
		if (lastMessage.type === TIM.TYPES.MSG_IMAGE) {
			return {
				lastMsg: '[图片]',
				lastDate: moment(lastMessage.lastTime * 1000).format(
					'YYYY-MM-DD'
				),
			};
		}
		//音频
		if (lastMessage.type === TIM.TYPES.MSG_AUDIO) {
			return {
				lastMsg: '[音频]',
				lastDate: moment(lastMessage.lastTime * 1000).format(
					'YYYY-MM-DD'
				),
			};
		}

		//视频
		if (lastMessage.type === TIM.TYPES.MSG_VIDEO) {
			return {
				lastMsg: '[视频]',
				lastDate: moment(lastMessage.lastTime * 1000).format(
					'YYYY-MM-DD'
				),
			};
		}
		//文件
		if (lastMessage.type === TIM.TYPES.MSG_FILE) {
			return {
				lastMsg: '[文件]',
				lastDate: moment(lastMessage.lastTime * 1000).format(
					'YYYY-MM-DD'
				),
			};
		}

		//自定义消息
		if (lastMessage.type === TIM.TYPES.MSG_CUSTOM) {
			return {
				lastMsg: '[自定义消息]',
				lastDate: moment(lastMessage.lastTime * 1000).format(
					'YYYY-MM-DD'
				),
			};
		}

		return {
			lastMsg: '',
			lastDate: '',
		};
	}
}

/**
 * 重置会话列表数据
 */
export async function resetConversationList() {
	let imResponse = await tim.getConversationList();

	store.commit(
		'imStore/RESET_CONVERSATIONLIST',
		imResponse.data.conversationList || []
	);
}
